// components/hlogin/hlogin.ts
import { DoLogin, DoUploadAvator } from '../../utils/httpcall';
Component({
  behaviors: ['wx://component-export'],
  options: {styleIsolation: 'apply-shared'},
  data: {
    title:"授权微信登陆",
    avatarUrl:"",
    nickName: "",
    errMsg:"请授权昵称和头像",
    hiddenError: true
  },
  methods:{
    onChooseAvatar(e:any){
      const { avatarUrl } = e.detail 
      this.setData({avatarUrl, })
    },
    onInput(e:any){
      const data = e.detail
      console.log(data)
      if(this.data.nickName === ""){
        this.setData({nickName:data.value})
      }
    },
    onLogin(){
      if(this.data.avatarUrl === "" || this.data.nickName === ""){
        this.setData({hiddenError: false})
      }else{
        // 登录
        wx.login({
          success: res => {
            DoLogin(res.code, this.data.nickName, this.data.avatarUrl, (_res:any)=>{
              this.setData({hiddenError: true})
              DoUploadAvator(this.data.avatarUrl)
              this.triggerEvent('onLogin', {}, { bubbles: true})
            }, (_res:any)=>{})
          },
        })
      }
    }
  }
})